package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baoyouqun.base.BaseCmd;
import com.baoyouqun.domain.CreateCmd.RoleCreateCmd;
import com.baoyouqun.domain.UpdateCmd.RolePermissionUpdateCmd;
import com.baoyouqun.domain.VO.MenuTreeVO;
import com.baoyouqun.domain.VO.RoleVO;
import com.baoyouqun.domain.cmd.UserRoleCmd;
import com.baoyouqun.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role-permission")
@Api(tags = "后台角色与权限配置接口")
public class RolePermissionController {

    @Resource
    private RoleService roleService;
    @Resource
    private RolePermissionService rolePermissionService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private UserService userService;
    @Resource
    private PermissionService permissionService;

    /**
     * 1. 创建角色并配置权限
     */
    @PostMapping("/role/create")
    @ApiOperation("创建角色并配置权限")
    public SingleResponse<RoleVO> createRoleWithPerm(@Valid @RequestBody RoleCreateCmd cmd) {
        return roleService.create(cmd);
    }

    /**
     * 2. 修改已有角色的权限
     */
    @PutMapping("/role/permission/update")
    @ApiOperation("修改角色权限（全量替换）")
    public Response updateRolePermission(@Valid @RequestBody RolePermissionUpdateCmd cmd) {
        return rolePermissionService.update(cmd);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/role/permission/delete")
    @ApiOperation("删除角色")
    @Transactional
    public Response updateRolePermission(@Valid BaseCmd cmd) {
        StpUtil.checkRole("R010000");
        if (roleService.removeById(cmd.getId())) {
            rolePermissionService.removeByRoleId(cmd.getId());
            return Response.buildSuccess();
        }
        throw new RuntimeException("删除角色失败");
    }

    /**
     * 3. 分配用户角色（限制单用户单角色）
     */
    @PostMapping("/user/role/assign")
    @ApiOperation("分配用户角色（单用户仅一个角色）")
    public Response assignUserRole(@Valid @RequestBody UserRoleCmd cmd) {
        return userRoleService.assignRoles(cmd);
    }

    /**
     * 4. 获取用户的权限菜单（树形结构）
     */
    @GetMapping("/user/permission/menu")
    @ApiOperation("根据用户ID获取权限菜单")
    public SingleResponse<MenuTreeVO> getUserPermissionMenu() {
        return permissionService.getUserPermissionMenu(StpUtil.getLoginIdAsString());
    }


    /**
     * 5. 获取角色的权限菜单（树形结构）
     */
    @GetMapping("/role/permission/menu")
    @ApiOperation("根据角色ID获取权限列表")
    public SingleResponse<MenuTreeVO> getRolePermissionMenu(String roleId) {
        return permissionService.getRolePermissionMenu(roleId);
    }

    /**
     * 6. 获取角色的权限菜单（树形结构）
     */
    @GetMapping("/role/list")
    @ApiOperation("角色ID列表")
    public SingleResponse<List<RoleVO>> getRoleList() {
        return roleService.getRoleList();
    }


}